﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tisbi.LawFirm.BL;
using Tisbi.LawFirm.BL.Services;
using Tisbi.LawFirm.DAL.Models;
using Tisbi.LawFirm.UI.Views.Controls.Forms;

namespace Tisbi.LawFirm.UI.ViewModel
{
    public class CourtsViewModel : BaseViewModel<Court, IServiceAgentBase<Court>>
    {
        private ObservableCollection<District> _districts;
        public ObservableCollection<District> Districts
        {
            get { return _districts; }
            set
            {
                _districts = value;
                RaisePropertyChanged(() => Districts);
            }
        }

        protected override void LoadModelBySearch()
        {
            IsBusy = true;

            ModelServiceAgent.GetItem
                (
                    (list, exception) =>
                        ModelLoadedByWhere(list, exception, item => item.Title.Contains(SearchCriteria))
                );
        }

        protected override void ShowEditDialog()
        {
            if (SelectedModelItem == null)
            {
                return;
            }
            LoadData();

            var model = new CourtCRUDViewModel(SelectedModelItem) { Districts = Districts };
            ShowEditDialog<CourtCRUDForm>(model, "Карточка изменения суда");
        }

        protected override void ShowCreatedDialog()
        {
            LoadData();

            var newItem = new Court();
            var detailModel = new CourtCRUDViewModel(newItem) { Districts = Districts };

            ShowCreatedDialog<CourtCRUDForm>(detailModel, "Карточка создания нового дела");
        }

        private void LoadData()
        {
            // Flip busy flag
            IsBusy = true;

            // Load items
            DistrictsServiceAgent.GetItem
                (
                    DistrictsLoaded
                );
        }

        private void DistrictsLoaded(List<District> entities, Exception error)
        {
            // If no error is returned, set the model to entities
            if (error == null)
            {
                Districts = new ObservableCollection<District>(entities);
            }
            // Otherwise notify view that there's an error
            else
            {
                Notifyer.NotifyError("Unable to retrieve items", error);
            }

            // Set SelectedItem to the first item
            if (Districts == null || Districts.Count <= 0)
            {
                HasVarnings = true;

                if (!string.IsNullOrWhiteSpace(Varnings))
                {
                    Varnings += "\n";
                }
                Varnings += "В базе отсудствуют федеральные округи, для продолжения работы добавьте в реестр новый [url=/Views/Pages/Districts.xaml|_top]федеральный округ[/url].";
            }

            // We're done
            IsBusy = false;

            SetCanProperties();
        }
    }
}
